﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class WarehouseService : BaseService, IWarehouseService
    {
        private readonly IWarehouseRepository _warehouserepository;
        private readonly IUnitOfWork _unitOfWork;
        public WarehouseService(IMapper mapper, IWarehouseRepository _warehouserepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            this._warehouserepository = _warehouserepository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 添加库房
        /// </summary>
        /// <param name="m"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddWarehouse(Warehouse m, out string message)
        {
            message = "";
            var aobj = _warehouserepository.QueryEntityWhere(x => x.Code == m.Code);
            if (aobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            _warehouserepository.AddEntity(m);
            return _unitOfWork.SaveChangesEntity();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelWarehouse(long id)
        {
            var wobj = _warehouserepository.QueryEntityById(id);
            if (wobj != null)
            {
                _warehouserepository.DelEntity(wobj);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        /// <summary>
        /// 列表显示
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<Warehouse> GetAllWarehouseList(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var wList = _warehouserepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                wList = wList.Where(c => c.Name.Contains(name));
            }
            var list = _warehouserepository.GetEntityAlls(wList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        /// <summary>
        /// 根据主键获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Warehouse? GetWarehouseById(long id)
        {
            return _warehouserepository.QueryEntityById(id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="wh"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool UpdWarehouse(Warehouse m, out string message)
        {
            message = "";
            var wobj =_warehouserepository.QueryIsEntityWhere(x => x.Id != m.Id && x.Code == m.Code);
            if (wobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            _warehouserepository.UpdEntity(m);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
